package com.qlslylq.ad.example.activity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.qlslylq.ad.example.R;
import com.qlslylq.ad.example.constant.KeyConst;
import com.qlslylq.ad.sdk.core.listener.BannerAdListener;
import com.qlslylq.ad.sdk.core.loader.BannerAdLoader;
import com.qlslylq.ad.sdk.core.rsp.AdError;
import com.qlslylq.ad.sdk.enums.AdSize;
import com.qlslylq.ad.sdk.util.DensityUtils;
import com.qlslylq.ad.sdk.util.Log;

/**
 * 横幅广告测试页<br/>
 */
public class BannerAdTestMainActivity extends BaseActivity {

    private BannerAdLoader loader;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner_ad_test_main);
    }

    protected void initView() {
        setTitle("横幅广告测试");
    }

    protected void initData() {
        initLoader();
    }

    protected void initListener() {

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_0:
                reverseScreenOrientation();
                break;
            case R.id.btn_1:
                loader.load();
                break;
            case R.id.btn_2:
                loader.show();
                break;
            case R.id.btn_3:
                loader.loadAndShow();
                break;
            case R.id.btn_4:
                loader.closeAd();
                loader.destroyAd();
                break;
            case R.id.btn_5:
                loader.closeAd();
                loader.destroyAd();
                break;
            case R.id.btn_6:
                showToast("正在开发中~");
                break;
        }
    }

    private void initLoader() {
        ViewGroup adContainer = findView(R.id.layout_ad_container);
        if (loader == null) {
            loader = new BannerAdLoader(this, KeyConst.JH_BANNER_POS_ID, adContainer, 0, AdSize.SIZE_640_180, new BannerAdListener() {
                public void onAdShow() {
                    Log.i("onAdShow");
                }

                public void onAdShowError(AdError error) {
                    Log.i("onAdShowError");
                    showToast("onAdShowError");
                }

                public void onAdDismiss() {
                    Log.i("onAdDismiss");
                    showToast("onAdDismiss");
                }

                public void onAdClick() {
                    Log.i("onAdClick");
                    showToast("onAdClick");
                }

                public void onDislikeShow() {

                }

                public void onDislikeSelected(String value) {
                    Log.i("onDislikeSelected：" + value);
                    showToast("onDislikeSelected：" + value);
                }

                public void onDislikeDismiss() {
                    Log.i("onDislikeDismiss");
                }

            });
            loader.setSlideIntervalTime(30 * 1000);
            loader.setAllowShowCloseBtn(true);
        }
    }

    /**
     * 横竖屏切换<br/>
     */
    private void reverseScreenOrientation() {
        boolean isLandscape = getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

        ViewGroup adContainer = findView(R.id.layout_ad_container);
        adContainer.getLayoutParams().height = isLandscape ? DensityUtils.dp2px(this, 230) : DensityUtils.dp2px(this, 150);

        setRequestedOrientation(isLandscape ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    protected void onDestroy() {
        super.onDestroy();
        loader.destroyAd();
    }

}